///////////////////////////
// 签名验证算法
// openssl genrsa  -out server.pem 1024
// openssl req -key server.pem -new -x509 -out cert.pem
///////////////////////////

var crypto = require('crypto')
    ,fs = require('fs');

// function signer(algorithm,key,data){
//     var sign = crypto.createSign(algorithm);
//     sign.update(data);
//     sig = sign.sign(key, 'hex');
//     return sig;
// }

function verify(algorithm,pub,sig,data){
    var verify = crypto.createVerify(algorithm);
    verify.update(data,'utf8');
    var decodedstr = new Buffer(sig, 'base64');
    return verify.verify(pubkey, decodedstr, 'hex')
}

var algorithm = 'RSA-SHA1';
var data = "cinema_code=TUOJIN";   //传输的数据
//var privatePem = fs.readFileSync('server.pem');
//var key = privatePem.toString();
//var sig = signer(algorithm,key,data); //数字签名

var publicPem = fs.readFileSync('rsa_public_key_nodejs.pem');
var pubkey = publicPem.toString();

var sig = 'W0RL1z5rlm7swIItWykgUSuIULsWbrzgmLdj2fofCu3BnQI99jcBQvL02uZ+CgjGe0w9xYnatKGF4Lkngr0ciymxWXGGjKgMT1dHYEYTN1DyTkrUfwN6LYTHjFhUUaH/BnooN4CHOCQ6N+QPPFwjihleQ0bqKfU09NLUXJUTg24=';
console.log(verify(algorithm,pubkey,sig,data));         //验证数据，通过公钥、数字签名 =》是原始数据
//console.log(verify(algorithm,pubkey,sig,data+"2"));    //验证数据，通过公钥、数字签名  =》不是原始数据